{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Arduino LCD Example using AdaFruit 1.8\" LCD Shield\n",
    "\n",
    "This notebook shows a demo on Adafruit 1.8\" LCD shield."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "from pynq.overlays.base import BaseOverlay\n",
    "base = BaseOverlay(\"base.bit\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 1. Instantiate AdaFruit LCD controller\n",
    "In this example, make sure that 1.8\" LCD shield from Adafruit is placed on the Arduino interface.\n",
    "\n",
    "After instantiation, users should expect to see a PYNQ logo with pink background shown on the screen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "from pynq.lib.arduino import Arduino_LCD18\n",
    "\n",
    "lcd = Arduino_LCD18(base.ARDUINO)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 2. Clear the LCD screen\n",
    "Clear the LCD screen so users can display other pictures."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "lcd.clear()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 3. Display a picture\n",
    "\n",
    "The screen is 160 pixels by 128 pixels. So the largest picture that can fit in the screen is 160 by 128. To resize a picture to a desired size, users can do:\n",
    "```python\n",
    "from PIL import Image\n",
    "img = Image.open('data/large.jpg')\n",
    "w_new = 160\n",
    "h_new = 128\n",
    "new_img = img.resize((w_new,h_new),Image.ANTIALIAS)\n",
    "new_img.save('data/small.jpg','JPEG')\n",
    "img.close()\n",
    "```\n",
    "The format of the picture can be PNG, JPEG, BMP, or any other format that can be opened using the `Image` library. In the API, the picture will be compressed into a binary format having (per pixel) 5 bits for blue, 6 bits for green, and 5 bits for red. All the pixels (of 16 bits each) will be stored in DDR memory and then transferred to the IO processor for display. \n",
    "\n",
    "The orientation of the picture is as shown below, while currently, only orientation 1 and 3 are supported. Orientation 3 will display picture normally, while orientation 1 will display picture upside-down.\n",
    "<img src=\"data/adafruit_lcd18.jpg\" width=\"400px\"/>\n",
    "\n",
    "To display the picture at the desired location, the position has to be calculated. For example, to display in the center a 76-by-25 picture with orientation 3, `x_pos` has to be (160-76/2)=42, and `y_pos` has to be (128/2)+(25/2)=76.\n",
    "\n",
    "The parameter `background` is a list of 3 components: [R,G,B], where each component consists of 8 bits. If it is not defined, it will be defaulted to [0,0,0] (black).  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "lcd.display('data/board_small.jpg',x_pos=0,y_pos=127,\n",
    "            orientation=3,background=[255,255,255])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 4. Animate the picture\n",
    "\n",
    "We can provide the number of frames to the method `display()`; this will move the picture around with a desired background color."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "lcd.display('data/logo_small.png',x_pos=0,y_pos=127,\n",
    "            orientation=1,background=[255,255,255],frames=100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 5. Draw a line\n",
    "\n",
    "Draw a white line from upper left corner towards lower right corner.\n",
    "\n",
    "The parameter `background` is a list of 3 components: [R,G,B], where each component consists of 8 bits. If it is not defined, it will be defaulted to [0,0,0] (black). \n",
    "\n",
    "Similarly, the parameter `color` defines the color of the line, with a default value of [255,255,255] (white).\n",
    "\n",
    "All the 3 `draw_line()` use the default orientation 3.\n",
    "\n",
    "Note that if the background is changed, the screen will also be cleared. Otherwise the old lines will still stay on the screen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "lcd.clear()\n",
    "lcd.draw_line(x_start_pos=151,y_start_pos=98,x_end_pos=19,y_end_pos=13)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Draw a 100-pixel wide red horizontal line, on a yellow background. Since the background is changed, the screen will be cleared automatically."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "lcd.draw_line(50,50,150,50,color=[255,0,0],background=[255,255,0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Draw a 80-pixel tall blue vertical line, on the same yellow background."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "lcd.draw_line(50,20,50,120,[0,0,255],[255,255,0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 6. Print a scaled character\n",
    "\n",
    "Users can print a scaled string at a desired position with a desired text color and background color.\n",
    "\n",
    "The first `print_string()` prints \"Hello, PYNQ!\" at 1st row, 1st column, with white text color and blue background.\n",
    "\n",
    "The second `print_string()` prints today's date at 5th row, 10th column, with yellow text color and blue background.\n",
    "\n",
    "Note that if the background is changed, the screen will also be cleared. Otherwise the old strings will still stay on the screen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "text = 'Hello, PYNQ!'\n",
    "lcd.print_string(1,1,text,[255,255,255],[0,0,255])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import time\n",
    "text = time.strftime(\"%d/%m/%Y\")\n",
    "lcd.print_string(5,10,text,[255,255,0],[0,0,255])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 7. Draw a filled rectangle\n",
    "\n",
    "The next 3 cells will draw 3 rectangles of different colors, respectively. All of them use the default black background and orientation 3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "lcd.draw_filled_rectangle(x_start_pos=10,y_start_pos=10,\n",
    "                          width=60,height=80,color=[64,255,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "lcd.draw_filled_rectangle(x_start_pos=20,y_start_pos=30,\n",
    "                          width=80,height=30,color=[255,128,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "lcd.draw_filled_rectangle(x_start_pos=90,y_start_pos=40,\n",
    "                          width=70,height=120,color=[64,0,255])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 8. Read joystick button"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Right\n"
     ]
    }
   ],
   "source": [
    "button=lcd.read_joystick()\n",
    "if button == 1:\n",
    "    print('Left')\n",
    "elif button == 2:\n",
    "    print('Down')\n",
    "elif button==3:\n",
    "    print('Center')\n",
    "elif button==4:\n",
    "    print('Right')\n",
    "elif button==5:\n",
    "    print('Up')\n",
    "else:\n",
    "    print('Not pressed')    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
